package com.neu.his.department.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.neu.his.commonutils.R;
import com.neu.his.department.entity.Department;
import com.neu.his.department.entity.DepartmentVo;
import com.neu.his.department.mapper.DepartmentMapper;
import com.neu.his.department.service.DepartmentService;

@CrossOrigin
@RequestMapping("/his/department")
@RestController
public class DepartmentController {
	
	@Autowired 
	private DepartmentService departmentService;
//		1.	对科室的添加
	@PostMapping("/addDepartment")
	public R addDepartment(@RequestBody Department department) {
		System.out.println(department);
		boolean addDepartment = departmentService.addDepartment(department);
		if(addDepartment) {
			return R.ok().message("添加成功");
		}else {
			return R.error().message("添加失败");
		}
		
		
		
	}
//	    2.	删除单个科室
	@PostMapping("/deleteDepartment")
	public R deleteDepartment(@RequestBody Map<String,Integer> params) {
		int id = params.get("id");
		boolean deleteDepartment = departmentService.deleteDepartment(id);
		if(deleteDepartment) {
			return R.ok().message("删除成功");
		}else {
			return R.error().message("删除失败");
		}
	
		
	}
//		3. 对科室的删除通过多个id
	@PostMapping("/deleteDepartmentByIds")
	public R deleteDepartmentByIds(@RequestBody Integer [] ids) {
		
		boolean deleteDepartmentByIds = departmentService.deleteDepartmentByIds(ids);
		if(deleteDepartmentByIds) {
			return R.ok().message("修改成功");
		}else {
			return R.error().message("修改失败");
		}

	}
//		4. 对科室的修改
	@PostMapping("/updateDepartment")
	public R updateDepartment(@RequestBody Department department) {
		System.out.println(department);
		boolean updateDepartment = departmentService.updateDepartment(department);
		if(updateDepartment) {
			return R.ok().message("修改成功");
		}else {
			return R.error().message("修改失败");
		}

		
	}
//		5.带条件的分页查询 支持根据科室编码，科室名和所属科室类型
	@GetMapping("/pageDepartmentQuery")
	public R pageDepartmentQuery(int page,int limit,String keyword,String keyword2,int keyword3) {
		page = (page-1)*limit;
		List<DepartmentVo> list=departmentService.pageDepartmentQuery(page, limit, keyword, keyword2, keyword3);
		 int total=departmentService.pageDepartmentTotal(keyword, keyword2, keyword3);
		 return R.ok().data("list",list).data("total",total);
		
		
	}
//		6.excel 导出
	@GetMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response) throws Exception{
		departmentService.exportExcel(response);
	
		
	}
//		7.execl 导入
	@PostMapping("/importExcel")
	public R importExcel(MultipartFile file) throws Exception{
		
		 if (file == null) {
	            return R.error().message("文件为空");
	        }
			departmentService.importExcel(file);
			return R.ok();	
		}
		
     	
	}

	
	

